<?php

namespace app\sysuser\controller;

use app\common\controller\SysBackend;
use app\common\service\UploadService;
use think\facade\View;

/**
 * 配置
 */
class Oss extends SysBackend
{
    protected array $noNeedLogin = ['*'];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function sts()
    {
        return redirect('/common_api/oss/sts');
    }

    /*
     * 文件上传
     * */
    public function uploadFile()
    {
        $file = $this->request->file('file');
        if (!$file) {
            $this->fail(__('请选择上传文件'));
        }
        // 获取文件基本信息
        $fileName = $file->getOriginalName();  // 原始文件名
        // 读取文件内容
        $UploadService = new UploadService();
        $path = $UploadService->uploadFile($file, $fileName);
        if (!$path) {
            $this->fail(__($UploadService->getError()));
        }
        $this->ok(__('Operation completed'), $path);
    }


    /*
     * 视频上传
     * */
    public function uploadVideo()
    {
        $file = $this->request->file('file');
        if (!$file) {
            $this->fail(__('请选择上传视频'));
        }

        // 获取文件基本信息
        $fileName = $file->getOriginalName();  // 原始文件名

        $UploadService = new UploadService();
        $path = $UploadService->uploadVideo($file, $fileName);
        if (!$path) {
            $this->fail(__($UploadService->getError()));
        }
        $this->ok(__('视频上传成功'), $path);
    }

    /*
     * 视频上传
     * */
    public function uploadImg()
    {
        $file = $this->request->file('file');
        if (!$file) {
            $this->fail(__('请选择上传图片'));
        }

        // 获取文件基本信息
        $fileName = $file->getOriginalName();  // 原始文件名

        // 读取文件内容
        $UploadService = new UploadService();
        $path = $UploadService->uploadImg($file, $fileName);
        if (!$path) {
            $this->fail(__($UploadService->getError()));
        }

        $this->ok(__('视频上传成功'), $path);
    }

    public function uploadAudio()
    {
        $file = $this->request->file('file');
        if (!$file) {
            $this->fail(__('请选择上传音频'));
        }

        // 获取文件基本信息
        $fileName = $file->getOriginalName();  // 原始文件名

        $UploadService = new UploadService();
        $path = $UploadService->uploadAudio($file, $fileName);
        if (!$path) {
            $this->fail(__('视频上传失败'));
        }

        $this->ok(__('视频上传成功'), $path);
    }

    /*
   * 视频上传
   * */
    public function imgRichText()
    {
        $file = $this->request->file('file');
        if (!$file) {
            $this->fail(__('请选择上传图片'));
        }

        // 获取文件基本信息
        $fileName = $file->getOriginalName();  // 原始文件名
        $UploadService = new UploadService();
        $path = $UploadService->uploadImg($file, $fileName);
        if (!$path) {
            $this->fail(__($UploadService->getError()));
        }
        return json([
            'errno' => 0,
            'data' => [
                'url' => $path
            ]
        ]);
    }
}